<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/23 1:27
 */

namespace app\admin\controller;


use app\admin\business\AdminGroup;
use app\admin\business\AdminRole as AdminRoleBus;
use app\admin\business\AdminUser;
use app\admin\validate\AdminRole as AdminRoleValidate;
use app\common\lib\Show;
use think\Exception;
use think\facade\View;

class AdminRole extends AdminBase {

    public $business = null;
    public function __construct() {
        $this->business = new AdminRoleBus();
    }

    public function index() {
        return View::fetch();
    }

    public function getLists() {
        $data = [];
        $limit = input('param.limit', 10, 'intval');
        $rolename = input('param.rolename', '', 'trim');

        if (!empty($rolename)) {
            $data['rolename'] = $rolename;
        }

        $result = (new AdminRoleBus())->getLists($data, $limit);
        return Show::show_lay_table(config('layui.success'), '', $result['total'], $result['data']);
    }

    // 添加角色
    public function add() {
        if(request()->isPost()){
            $data = input('param.');
            // 使用 validate 验证机制验证参数
            $validate = new AdminRoleValidate();
            if (!$validate->scene('insert')->check($data)) {
                return Show::error($validate->getError());
            }

            // 执行 business 操作
            $res = (new AdminRoleBus())->insertData($data);
            if (!$res) {
                return Show::error('添加角色失败');
            }
            return Show::success('添加角色成功');
        } else {
            return View::fetch();
        }
    }

    // 编辑角色
    public function edit() {
        if(request()->isPost()){
            $data = input('param.');
            // 使用 validate 验证机制验证参数
            $validate = new AdminRoleValidate();
            if (!$validate->scene('update')->check($data)) {
                return Show::error($validate->getError());
            }

            // 执行 business 操作
            $res = (new AdminRoleBus())->updateData($data);
            if (!$res) {
                return Show::error('更新角色失败');
            }
            return Show::success('更新角色成功');

        } else {
            $id = input('param.id', 0, 'intval');
            if ($id == 0) {
                throw new Exception('请求不合法');
            }
            $info = (new AdminRoleBus())->getById($id);
            return View::fetch('', [
                'info' => $info
            ]);
        }
    }

    // 权限配置
    public function auth(){
        if(request()->isPost()){

            $data = input('param.');
            $data['rules'] = implode(',', $data['authids']);
            unset($data['authids']);

            // 使用 validate 验证机制验证参数
            $validate = new AdminRoleValidate();
            if (!$validate->scene('auth')->check($data)) {
                return Show::error($validate->getError());
            }

            // 执行 business 操作
            $res = (new AdminRoleBus())->updateData($data);
            if (!$res) {
                return Show::error('更新权限失败');
            }
            return Show::success('更新权限成功');

        } else {
            $id = input('param.id', 0, 'intval');
            if ($id == 0) {
                throw new Exception('请求不合法');
            }
            $info = (new AdminRoleBus())->getById($id);
            return View::fetch('', [
                'info' => $info,
            ]);
        }
    }

    // 角色管理员
    public function user() {
        $id = input('param.id', 0, 'intval');
        if (!$id) {
            throw new Exception('请求不合法');
        }
        return View::fetch('', [
            'id' => $id
        ]);
    }

    // 角色下，获取对应管理员
    public function getRoleToAdmin() {
        $id = input('param.id', 0, 'intval');
        if (!$id) {
            throw new Exception('请求不合法');
        }
        $ids = (new AdminGroup())->getByCondition(['roleid' => $id], ["adminid" => "ASC"]);
        $adminids = array_column($ids, 'adminid');

        $list = (new AdminUser())->getByIds($adminids);

        return Show::show_lay_table(config('layui.success'), '', 0, $list);
    }

}